/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of JMail                                              *
 * Copyright (C) 2002-2003 Yvan Norsa <norsay@wanadoo.fr>                  *
 *                                                                         *
 * JMail is free software; you can redistribute it and/or modify           *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 2 of the License, or       *
 * any later version.                                                      *
 *                                                                         *
 * JMail is distributed in the hope that it will be useful,                *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License along *
 * with JMail; if not, write to the Free Software Foundation, Inc.,        *
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                 *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.security.*;
import java.util.*;
import javax.swing.*;
import sun.misc.*;

/** Create a new profile */
final class CreateProfile extends JDialog
{
    /** Language resource */
    private ResourceBundle msgBundle;

    /** Main panel */
    private JPanel panel;

    /** *Dynamic* label */
    private JPanel content;

    /** Panel for the buttons */
    private JPanel buttons;

    private JButton next;
    private JButton finish;

    /** Profile name */
    private String profile;

    private boolean ldapUser;

    /** Allows to know which step has been reached */
    private int state;

    /** "Enter your name" */
    private JLabel firstText;
    private JTextField firstName;

    private JLabel firstProfilePasswordLabel;
    private JPasswordField firstProfilePasswordField;
        
    /** "Enter your email" */
    private JLabel secondText;
    private JTextField secondAddress;

    /** "Please choose your server type :" */
    private JLabel thirdServer;

    /** Servers type */
    private String thirdServers[];

    /** Combo box for the server type */
    private JComboBox thirdTypes;

    /** "Incoming server :" */
    private JLabel thirdIncomingServerLabel;

    private JLabel thirdIncomingPortLabel;

    private JLabel thirdUseSSLLabel;

    /** Textfield for the incoming server */
    private JTextField thirdIncomingServerField;

    /** Textfield for the incoming server's port */
    private JTextField thirdIncomingPortField;

    private JCheckBox thirdUseSSLBox;

    /** "Outgoing server (SMTP) :" */
    private JLabel thirdOutgoingServerLabel;

    private JLabel thirdOutgoingPortLabel;

    /** Textfield for the outgoing server */
    private JTextField thirdOutgoingServerField;

    /** Textfield for the outgoing server's port */
    private JTextField thirdOutgoingPortField;

    /** "User name :" */
    private JLabel fourthName;

    /** Textfield for the login */
    private JTextField fourthLoginField;

    /** "Password :" */
    private JLabel fourthPwd;

    /** Textfield for the password */
    private JPasswordField fourthPasswordField;

    /** "Congratulations !" */
    private JLabel lastText1;

    /** "Your profile has been created !" */
    private JLabel lastText2;

    private JLabel lastText3;
    private JLabel lastText4;

    private JLabel fifthQueryText;
    private JLabel fifthQueryPortNumber;
    private JTextField fifthQueryField;
    private JTextField fifthQueryPort;

    private JLabel fifthSuffixText;
    private JTextField fifthSuffixField;
    private JLabel fifthSuffixAddText;
    private JCheckBox fifthURLSuffix;
    private JCheckBox fifthBaseSuffix;
    private JCheckBox fifthLoginSuffix;

    private JLabel fifthBaseText;
    private JTextField fifthBaseField;

    private JLabel fifthLoginText;
    private JTextField fifthLoginField;
    
    private JLabel fifthPasswordText;
    private JPasswordField fifthPasswordField;

    private JLabel fifthNameText;

    private JTextField fifthNameField;

    private JLabel fifthMailText;

    private JTextField fifthMailField;

    private JLabel fifthSupText;
    private JTextField fifthSupField;
    private JButton fifthSupAdd;
    private JList fifthSupList;
    private JScrollPane fifthSupListPane;
    private Vector supFields;

    /** Listener used for the whole profile creation process */
    private CreateProfileListener listener;

    /** Constructor
     *  @param msgBundle language resource
     */
    protected CreateProfile(ResourceBundle msgBundle)
    {
	/** JDialog init */
	super((JFrame)null, msgBundle.getString("CreateProfile.frameTitle"), true);

	this.msgBundle = msgBundle;

	profile = new String();
	ldapUser = false;

	/** First step */
	state = 0;

	listener = new CreateProfileListener();

	panel = new JPanel(new BorderLayout());
	content = new JPanel();

	/** Creating the first panel */
	createFirstPanel();
	panel.add("Center", content);

	/** Init for buttons */
	buttons = new JPanel();

	next = new JButton(msgBundle.getString("CreateProfile.nextLabel"));
	next.setEnabled(false);
	next.addActionListener(listener);
	buttons.add(next);

	finish = new JButton(msgBundle.getString("CreateProfile.finishLabel"));
	finish.setEnabled(false);
	finish.addActionListener(listener);
	buttons.add(finish);

	panel.add("South", buttons);
	setContentPane(panel);

	setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

	setSize(400, 200);
	show();
    }

    /** Init the panel for the first step */
    private void createFirstPanel()
    {
	content = new JPanel();

	firstText = new JLabel(msgBundle.getString("CreateProfile.firstTextLabel"));
	content.add(firstText);

	firstName = new JTextField(20);
	firstName.addKeyListener(listener);
	content.add(firstName);

	firstProfilePasswordLabel = new JLabel(msgBundle.getString("CreateProfile.profilePasswordText"));
	content.add(firstProfilePasswordLabel);

	firstProfilePasswordField = new JPasswordField(20);
	firstProfilePasswordField.addKeyListener(listener);
	content.add(firstProfilePasswordField);
    }

    /** Init the panel for the second step */
    private void createSecondPanel()
    {
	content = new JPanel();

	secondText = new JLabel(msgBundle.getString("CreateProfile.secondTextLabel"));
	content.add(secondText);

	secondAddress = new JTextField(20);
	secondAddress.setText(firstName.getText() + "@");
	secondAddress.addKeyListener(listener);
	content.add(secondAddress);
    }

    /** Init the panel for the third step */
    private void createThirdPanel()
    {
	content = new JPanel();

	GridBagLayout gridBag = new GridBagLayout();
	GridBagConstraints constraints = new GridBagConstraints();
	content.setLayout(gridBag);

	Common.buildConstraints(constraints, 0, 0, 1, 1, 80, 100);
	thirdServer = new JLabel(msgBundle.getString("CreateProfile.thirdServerLabel"));
	gridBag.setConstraints(thirdServer, constraints);
	content.add(thirdServer);

	thirdServers = new String[2];
	thirdServers[0] = "pop3";
	thirdServers[1] = "imap";
             
	Common.buildConstraints(constraints, 1, 0, 1, 1, 20, 0);
	thirdTypes = new JComboBox(thirdServers);
	thirdTypes.setEditable(false);
	thirdTypes.addActionListener(listener);
	gridBag.setConstraints(thirdTypes, constraints);
	content.add(thirdTypes);

	Common.buildConstraints(constraints, 0, 1, 1, 1, 0, 0);
	thirdIncomingServerLabel = new JLabel(msgBundle.getString("CreateProfile.thirdIncomingServerLabel"));
	gridBag.setConstraints(thirdIncomingServerLabel, constraints);
	content.add(thirdIncomingServerLabel);

	Common.buildConstraints(constraints, 1, 1, 1, 1, 0, 0);
	thirdIncomingPortLabel = new JLabel("Port :");
	gridBag.setConstraints(thirdIncomingPortLabel, constraints);
	content.add(thirdIncomingPortLabel);

	Common.buildConstraints(constraints, 2, 1, 1, 1, 0, 0);
	thirdUseSSLLabel = new JLabel(msgBundle.getString("CreateProfile.useSSLText"));
	gridBag.setConstraints(thirdUseSSLLabel, constraints);
	content.add(thirdUseSSLLabel);

	Common.buildConstraints(constraints, 0, 2, 1, 1, 0, 0);
	thirdIncomingServerField = new JTextField(20);
	thirdIncomingServerField.addKeyListener(listener);
	gridBag.setConstraints(thirdIncomingServerField, constraints);
	content.add(thirdIncomingServerField);

	Common.buildConstraints(constraints, 1, 2, 1, 1, 0, 0);
	thirdIncomingPortField = new JTextField(4);
	thirdIncomingPortField.setText("110");
	thirdIncomingPortField.addKeyListener(listener);
	gridBag.setConstraints(thirdIncomingPortField, constraints);
	content.add(thirdIncomingPortField);

	Common.buildConstraints(constraints, 2, 2, 1, 1, 0, 0);
	thirdUseSSLBox = new JCheckBox();
	thirdUseSSLBox.addItemListener(listener);
	gridBag.setConstraints(thirdUseSSLBox, constraints);
	content.add(thirdUseSSLBox);

	Common.buildConstraints(constraints, 0, 3, 1, 1, 0, 0);
	thirdOutgoingServerLabel = new JLabel(msgBundle.getString("CreateProfile.thirdOutgoingServerLabel"));
	gridBag.setConstraints(thirdOutgoingServerLabel, constraints);
	content.add(thirdOutgoingServerLabel);

	Common.buildConstraints(constraints, 1, 3, 1, 1, 0, 0);
	thirdOutgoingPortLabel = new JLabel("Port :");
	gridBag.setConstraints(thirdOutgoingPortLabel, constraints);
	content.add(thirdOutgoingPortLabel);

	Common.buildConstraints(constraints, 0, 4, 1, 1, 0, 0);
	thirdOutgoingServerField = new JTextField(20);
	thirdOutgoingServerField.addKeyListener(listener);
	gridBag.setConstraints(thirdOutgoingServerField, constraints);
	content.add(thirdOutgoingServerField);

	Common.buildConstraints(constraints, 1, 4, 1, 1, 0, 0);
	thirdOutgoingPortField = new JTextField(4);
	thirdOutgoingPortField.setText("25");
	thirdOutgoingPortField.addKeyListener(listener);
	gridBag.setConstraints(thirdOutgoingPortField, constraints);
	content.add(thirdOutgoingPortField);
    }

    /** Create the panel for the fourth step */
    private void createFourthPanel()
    {
	content = new JPanel();

	GridBagLayout gridBag = new GridBagLayout();
	GridBagConstraints constraints = new GridBagConstraints();
	content.setLayout(gridBag);

	Common.buildConstraints(constraints, 0, 0, 1, 1, 100, 100);
	fourthName = new JLabel(msgBundle.getString("CreateProfile.fourthNameLabel"));
	gridBag.setConstraints(fourthName, constraints);
	content.add(fourthName);

	Common.buildConstraints(constraints, 0, 1, 1, 1, 0, 0);
	fourthLoginField = new JTextField(20);
	fourthLoginField.setText(firstName.getText());
	fourthLoginField.addKeyListener(listener);
	gridBag.setConstraints(fourthLoginField, constraints);
	content.add(fourthLoginField);

	Common.buildConstraints(constraints, 0, 2, 1, 1, 0, 0);
	fourthPwd = new JLabel(msgBundle.getString("CreateProfile.fourthPwdLabel"));
	gridBag.setConstraints(fourthPwd, constraints);
	content.add(fourthPwd);

	Common.buildConstraints(constraints, 0, 3, 1, 1, 0, 0);
	fourthPasswordField = new JPasswordField(20);
	fourthPasswordField.addKeyListener(listener);
	gridBag.setConstraints(fourthPasswordField, constraints);
	content.add(fourthPasswordField);
    }

    /** Create the *last* panel  */
    private void createLastPanel()
    {
	content = new JPanel();

	lastText1 = new JLabel(msgBundle.getString("CreateProfile.lastText1Label"));
	content.add(lastText1);

	lastText2 = new JLabel(msgBundle.getString("CreateProfile.lastText2Label"));
	content.add(lastText2);

	lastText3 = new JLabel(msgBundle.getString("CreateProfile.lastText3Label"));
	content.add(lastText3);

	lastText4 = new JLabel(msgBundle.getString("CreateProfile.lastText4Label"));
	content.add(lastText4);
    }

    /** Create the ldap panel */
    private void createFifthPanel()
    {
	content = new JPanel();

	GridBagLayout gridBag = new GridBagLayout();
	GridBagConstraints constraints = new GridBagConstraints();
	content.setLayout(gridBag);

	Common.buildConstraints(constraints, 0, 0, 1, 1, 80, 100);
	fifthQueryText = new JLabel(msgBundle.getString("CreateProfile.fifthQueryTextLabel"));
	gridBag.setConstraints(fifthQueryText, constraints);
	content.add(fifthQueryText);

	Common.buildConstraints(constraints, 1, 0, 1, 1, 20, 0);
	fifthQueryPortNumber = new JLabel("Port :");
	gridBag.setConstraints(fifthQueryPortNumber, constraints);
	content.add(fifthQueryPortNumber);

	Common.buildConstraints(constraints, 0, 1, 1, 1, 0, 0);
	fifthQueryField = new JTextField(20);
	fifthQueryField.setText("ldap://");
	fifthQueryField.addKeyListener(listener);
	gridBag.setConstraints(fifthQueryField, constraints);
	content.add(fifthQueryField);

	Common.buildConstraints(constraints, 1, 1, 1, 1, 0, 0);
	fifthQueryPort = new JTextField(4);
	fifthQueryPort.setText("389");
	fifthQueryPort.addKeyListener(listener);
	gridBag.setConstraints(fifthQueryPort, constraints);
	content.add(fifthQueryPort);
    
	Common.buildConstraints(constraints, 0, 2, 1, 1, 0, 0);
	fifthSuffixText = new JLabel(msgBundle.getString("CreateProfile.fifthSuffixTextLabel"));
	gridBag.setConstraints(fifthSuffixText, constraints);
	content.add(fifthSuffixText);

	Common.buildConstraints(constraints, 0, 3, 1, 1, 0, 0);
	fifthSuffixField = new JTextField(20);
	fifthSuffixField.addKeyListener(listener);
	gridBag.setConstraints(fifthSuffixField, constraints);
	content.add(fifthSuffixField);

	Common.buildConstraints(constraints, 0, 4, 1, 1, 0, 0);
	fifthSuffixAddText = new JLabel(msgBundle.getString("CreateProfile.fifthSuffixAddTextLabel"));
	gridBag.setConstraints(fifthSuffixAddText, constraints);
	content.add(fifthSuffixAddText);

	Common.buildConstraints(constraints, 0, 5, 1, 1, 0, 0);
	fifthURLSuffix = new JCheckBox("URL");
	gridBag.setConstraints(fifthURLSuffix, constraints);
	content.add(fifthURLSuffix);

	Common.buildConstraints(constraints, 1, 5, 1, 1, 0, 0);
	fifthBaseSuffix = new JCheckBox("Base", true);
	gridBag.setConstraints(fifthBaseSuffix, constraints);
	content.add(fifthBaseSuffix);

	Common.buildConstraints(constraints, 2, 5, 1, 1, 0, 0);
	fifthLoginSuffix = new JCheckBox("Login", true);
	gridBag.setConstraints(fifthLoginSuffix, constraints);
	content.add(fifthLoginSuffix);

	Common.buildConstraints(constraints, 0, 6, 1, 1, 0, 0);
	fifthBaseText = new JLabel(msgBundle.getString("CreateProfile.fifthBaseTextLabel"));
	gridBag.setConstraints(fifthBaseText, constraints);
	content.add(fifthBaseText);

	Common.buildConstraints(constraints, 0, 7, 1, 1, 0, 0);
	fifthBaseField = new JTextField(20);
	fifthBaseField.addKeyListener(listener);
	gridBag.setConstraints(fifthBaseField, constraints);
	content.add(fifthBaseField);

	Common.buildConstraints(constraints, 0, 8, 1, 1, 0, 0);
	fifthLoginText = new JLabel("Login :");
	gridBag.setConstraints(fifthLoginText, constraints);
	content.add(fifthLoginText);

	Common.buildConstraints(constraints, 0, 9, 1, 1, 0, 0);
	fifthLoginField = new JTextField(20);
	fifthLoginField.addKeyListener(listener);
	gridBag.setConstraints(fifthLoginField, constraints);
	content.add(fifthLoginField);

	Common.buildConstraints(constraints, 0, 10, 1, 1, 0, 0);
	fifthPasswordText = new JLabel(msgBundle.getString("CreateProfile.fifthLdapPasswordText"));
	gridBag.setConstraints(fifthPasswordText, constraints);
	content.add(fifthPasswordText);

	Common.buildConstraints(constraints, 0, 11, 1, 1, 0, 0);
	fifthPasswordField = new JPasswordField(20);
	fifthPasswordField.addKeyListener(listener);
	gridBag.setConstraints(fifthPasswordField, constraints);
	content.add(fifthPasswordField);

	Common.buildConstraints(constraints, 0, 12, 1, 1, 0, 0);
	fifthNameText = new JLabel(msgBundle.getString("CreateProfile.fifthNameTextLabel"));
	gridBag.setConstraints(fifthNameText, constraints);
	content.add(fifthNameText);
                
	Common.buildConstraints(constraints, 0, 13, 1, 1, 0, 0);
	fifthNameField = new JTextField(20);
	fifthNameField.addKeyListener(listener);
	gridBag.setConstraints(fifthNameField, constraints);
	content.add(fifthNameField);

	Common.buildConstraints(constraints, 0, 14, 1, 1, 0, 0);
	fifthMailText = new JLabel(msgBundle.getString("CreateProfile.fifthMailTextLabel"));
	gridBag.setConstraints(fifthMailText, constraints);
	content.add(fifthMailText);

	Common.buildConstraints(constraints, 0, 15,  1, 1, 0, 0);
	fifthMailField = new JTextField(20);
	fifthMailField.addKeyListener(listener);
	gridBag.setConstraints(fifthMailField, constraints);
	content.add(fifthMailField);
	
	Common.buildConstraints(constraints, 0, 16, 1, 1, 0, 0);
	fifthSupText = new JLabel(msgBundle.getString("CreateProfile.fifthSupTextLabel"));
	gridBag.setConstraints(fifthSupText, constraints);
	content.add(fifthSupText);

	Common.buildConstraints(constraints, 0, 17, 1, 1, 0, 0);
	fifthSupField = new JTextField(10);
	gridBag.setConstraints(fifthSupField, constraints);
	content.add(fifthSupField);

	Common.buildConstraints(constraints, 1, 17, 1, 1, 0, 0);
	fifthSupAdd = new JButton(msgBundle.getString("CreateProfile.fifthSupAddLabel"));
	fifthSupAdd.addActionListener(listener);
	gridBag.setConstraints(fifthSupAdd, constraints);
	content.add(fifthSupAdd);

	supFields = new Vector();
	Common.buildConstraints(constraints, 2, 17, 1, 1, 0, 0);
	fifthSupList = new JList();
	fifthSupListPane = new JScrollPane(fifthSupList);
	gridBag.setConstraints(fifthSupListPane, constraints);
	content.add(fifthSupListPane);
    }

    /** Returns the profile just created
     *  @return user profile
     */
    protected final String getProfile()
    {
	return(profile);
    }

    /** Listener for this class */
    private final class CreateProfileListener extends KeyAdapter implements ActionListener, ItemListener
    {
	/** This method is invoked when an event is triggered
	 *  @param e event
	 */
	public final void actionPerformed(ActionEvent e)
	{
	    JComponent c = (JComponent)e.getSource();

	    if(c == thirdTypes)
	    {
		String choice = (String)thirdTypes.getSelectedItem();

		if(choice.compareTo("pop3") == 0)
		    thirdIncomingPortField.setText("110");

		else if(choice.compareTo("imap") == 0)
		    thirdIncomingPortField.setText("143");

		String in = thirdIncomingServerField.getText();
		
		String out = thirdOutgoingServerField.getText();
		String outPort = thirdOutgoingPortField.getText();

		if(in.compareTo("") != 0 && out.compareTo("") != 0 && outPort.compareTo("") != 0)
		    next.setEnabled(true);

		else
		    next.setEnabled(false);
	    }

	    else if(c == fifthSupAdd)
	    {
		String newField = fifthSupField.getText().trim();

		if(newField.compareTo("") != 0)
		{
		    supFields.add(newField);
		    fifthSupList.setListData(supFields);
		    fifthSupField.setText("");
		}
	    }

	    else if(c == next)
	    {
		switch(state)
		{
		case 0:
		    panel.remove(content);
		    createSecondPanel();
		    state++;
		    panel.add("Center", content);
		    next.setEnabled(false);

		    break;

		case 1: 
		    panel.remove(content);
		    createThirdPanel();
		    state++;
		    panel.add("Center", content);
		    next.setEnabled(false);

		    break;

		case 2:
		    panel.remove(content);
		    createFourthPanel();
		    state++;
		    panel.add("Center", content);

		    String choice = (String)thirdTypes.getSelectedItem();
		    
		    //there MUST be a password when using pop3 (even if the account doesn't have one)
		    if(choice.compareTo("pop3") == 0)
			next.setEnabled(false);

		    break;

		case 3:
		    panel.remove(content);
		    createLastPanel();
		    state++;
		    panel.add("Center", content);
		    finish.setEnabled(true);

		    break;

		case 4:
		    ldapUser = true;
		    setSize(700, 400);
		    panel.remove(content);
		    createFifthPanel();
		    state++;
		    panel.add("Center", content);
		    next.setEnabled(false);
		    pack();

		    break;
		}

		setVisible(true);
	    }

	    else if(c == finish)
	    {
		Profile p = null;

		profile = firstName.getText().trim();

		char profilePass[] = firstProfilePasswordField.getPassword();
		String profilePassword = String.valueOf(profilePass).trim();

		String email = secondAddress.getText().trim();
		String type = (String)thirdTypes.getSelectedItem();
		String in = thirdIncomingServerField.getText().trim();
		String inPort = thirdIncomingPortField.getText().trim();
		boolean useSSL = thirdUseSSLBox.isSelected();
		String out = thirdOutgoingServerField.getText().trim();
		String outPort = thirdOutgoingPortField.getText().trim();
		String login = fourthLoginField.getText().trim();
		char pass[] = fourthPasswordField.getPassword();
		String password = String.valueOf(pass).trim();

		/** Computes the hashed profile password to store it */
		String encodedPass = JMailSecurity.hashAndEncode(profilePassword);
		/** Crypts the mail password using the clear profile password as key */
		String cryptedMailPassword = JMailSecurity.cryptAndEncode(profilePassword, password);

		if(!ldapUser)
		    p = new Profile(profile, encodedPass, email, (String)null, type, in, Integer.parseInt(inPort), useSSL, out, Integer.parseInt(outPort), login, cryptedMailPassword, (String)null, ldapUser, (Vector)null, (Vector)null);

		else
		{
		    String url = fifthQueryField.getText().trim();
		      
		    int port = Integer.parseInt(fifthQueryPort.getText().trim());

		    String suffix = fifthSuffixField.getText().trim();

		    boolean addSuffixToURL = fifthURLSuffix.isSelected();
		    boolean addSuffixToBase = fifthBaseSuffix.isSelected();
		    boolean addSuffixToLogin = fifthLoginSuffix.isSelected();
			
		    String base = fifthBaseField.getText().trim();
		    String ldapLogin = fifthLoginField.getText().trim();

		    char ldapPass[] = fifthPasswordField.getPassword();
		    String ldapPassword = String.valueOf(ldapPass).trim();

		    String name = fifthNameField.getText().trim();
		    String mail = fifthMailField.getText().trim();
			
		    /** For the last panel, the "finish" button is always enabled, to allow the user who misclicked on "next" to save his profile without having to enter false LDAP infos.. But we save it only if everything is OK */
		    if(url.compareTo("") != 0 && suffix.compareTo("") != 0 && base.compareTo("") != 0 && name.compareTo("") != 0 && mail.compareTo("") != 0)
		    {
			int nb = supFields.size();

			String[] fields = new String[nb];
			for(int i = 0; i < nb; i++)
			    fields[i] = (String)supFields.get(i);

			p = new Profile(profile, encodedPass, email, (String)null, type, in, Integer.parseInt(inPort), useSSL, out, Integer.parseInt(outPort), login, cryptedMailPassword, (String)null, ldapUser, url, port, suffix, addSuffixToURL, addSuffixToBase, addSuffixToLogin, base, ldapLogin, ldapPassword, name, mail, nb, fields, (Vector)null, (Vector)null);
		    }
		    
		    else
			p = new Profile(profile, encodedPass, email, (String)null, type, in, Integer.parseInt(inPort), useSSL, out, Integer.parseInt(outPort), login, cryptedMailPassword, (String)null, ldapUser, (Vector)null, (Vector)null);
		}

		/** Actually write the profile file */
		p.save();

		hide();
	    }
	}

	/** Method called when the user types a key
	 *  @param e event run
	 */
	public final void keyReleased(KeyEvent e)
	{
	    if(e.getKeyCode() == KeyEvent.VK_ENTER)
	    {
		if(next.isEnabled())
		    next.doClick();
	    }

	    else
	    {
		JTextField f = (JTextField)e.getSource();

		if(f == firstName || f == firstProfilePasswordField)
		{
		    String name = firstName.getText();
		    char[] profilePass = firstProfilePasswordField.getPassword();
		    String profilePassword = new String(profilePass);

		    if(name.compareTo("") != 0 && profilePassword.compareTo("") != 0)
			next.setEnabled(true);

		    else
			next.setEnabled(false);
		}

		else if(f == secondAddress)
		{
		    String address = secondAddress.getText();

		    if(address.compareTo("") != 0)
			next.setEnabled(true);

		    else
			next.setEnabled(false);
		}

		else if(f == thirdIncomingServerField || f == thirdIncomingPortField || f == thirdOutgoingServerField || f == thirdOutgoingPortField)
		{
		    String in = thirdIncomingServerField.getText();
		    String inPort = thirdIncomingPortField.getText();
		    String out = thirdOutgoingServerField.getText();
		    String outPort = thirdOutgoingPortField.getText();

		    if(in.compareTo("") != 0 && inPort.compareTo("") != 0 && out.compareTo("") != 0 && outPort.compareTo("") != 0)
			next.setEnabled(true);

		    else
			next.setEnabled(false);
		}

		else if(f == fourthLoginField || f == fourthPasswordField)
		{
		    String login = fourthLoginField.getText();

		    if(login.compareTo("") != 0)
			next.setEnabled(true);
		
		    else
			next.setEnabled(false);

		    String choice = (String)thirdTypes.getSelectedItem();

		    if(choice.compareTo("pop3") == 0)
		    {
			char pass[] = fourthPasswordField.getPassword();
			String password = new String(pass);

			if(password.compareTo("") == 0)
			    next.setEnabled(false);
		    }
		}
	    }
	}

	public final void itemStateChanged(ItemEvent e)
	{
	    if(thirdUseSSLBox.isSelected())
	    {
		if(((String)thirdTypes.getSelectedItem()).compareTo("pop3") == 0)
		    thirdIncomingPortField.setText("995");

		else //if(((String)thirdTypes.getSelectedItem()).compareTo("imap") == 0)
		    thirdIncomingPortField.setText("993");
	    }

	    else
	    {
		if(((String)thirdTypes.getSelectedItem()).compareTo("pop3") == 0)
		    thirdIncomingPortField.setText("110");

		else //if(((String)thirdTypes.getSelectedItem()).compareTo("imap") == 0)
		    thirdIncomingPortField.setText("143");
	    }
	}
    }
}
